import AutoChart from '@/components/Echarts/AutoChart';
// @ts-ignore
import { medicinePrescription } from '@/services/api';
import React, { useEffect } from 'react';
import { useImmer } from 'use-immer';

const ColumnCharts: React.FC = () => {
  const [options, setOptions] = useImmer({
    // title: {
    //   text: '文章类型占比',
    //   // left: 'center'
    // },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985'
        }
      }
    },
    legend: {
      // left: 'left',
      textStyle: {
        color: '#fff'
      }
    },
    xAxis: {
      type: 'category',
      data: []
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [],
        type: 'bar',
        color: '#8378ea',
        showBackground: true,
        barGap: 100,
        itemStyle: {
          borderRadius: [40, 40, 0, 0]
        },
        backgroundStyle: {
          color: 'rgba(180, 180, 180, 0.2)'
        }
      }
    ]
  });

  useEffect(() => {
    medicinePrescription().then((res) => {
      setOptions((draft) => {
        draft.xAxis.data = res.data.map((i: API.ChartsData) => i.name)
        draft.series[0].data = res.data.map((i: API.ChartsData) => Number(i.value))
      });
    });
  }, []);

  return <AutoChart option={options as any}></AutoChart>;
};
export default ColumnCharts;
